package com.help10000.rms.ui.activitys

import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.amir.common.beans.*
import com.amir.common.utils.DensityUtil
import com.amir.common.utils.ToastUtil
import com.amir.common.weight.DuplicateClickProxy
import com.amir.common.weight.SpaceItemDecoration
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.PickingPresenter
import com.help10000.rms.mvp.view.PickingView
import com.help10000.rms.ui.adapter.PickingAdapter
import com.help10000.rms.ui.base.BaseActivity
import com.help10000.rms.ui.windows.SelectPickingPopuWindow
import kotlinx.android.synthetic.main.activity_address.*
import kotlinx.android.synthetic.main.activity_picking.*


/**
 * 领料
 */
class PickingMaterialActivity : BaseActivity<PickingPresenter, PickingView>(),
    View.OnClickListener, PickingView {
    var repairInfo: RepairInfo? = null
    var currentWareId = ""//当前仓库ID
    var wareList = mutableListOf<Ware>()
    var pickingAdapter: PickingAdapter? = null
    val list = mutableListOf<SaleSheetDetail>()
    var isRefresh = false
    override fun getlayoutId(): Int {
        return R.layout.activity_picking
    }

    override fun initView() {
        //    initRefreshLayout(refresh)
        val linearLayoutManager = LinearLayoutManager(mContext)
        rv_list.layoutManager = linearLayoutManager

        pickingAdapter = PickingAdapter(mContext, list, R.layout.item_picking)
        rv_list.adapter = pickingAdapter
        rv_list.addItemDecoration(SpaceItemDecoration(0, 10))

        refresh.setColorSchemeColors(
            resources.getColor(R.color.colorPrimary),
            resources.getColor(R.color.colorPrimary),
            resources.getColor(R.color.colorPrimary),
            resources.getColor(R.color.colorPrimary)
        )
        refresh.setOnRefreshListener {
            isRefresh = true
            downOnRefresh()
        }
    }

    override fun showLoading() {
        if (!isRefresh)
            super.showLoading()
    }

    override fun onResume() {
        super.onResume()
        mPresenter.pickingListData(repairInfo?.ID!!)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        outState.putSerializable("data", repairInfo)
        super.onSaveInstanceState(outState)
    }

    override fun initData(savedInstanceState: Bundle?) {
        if (savedInstanceState == null)
            repairInfo = intent.getSerializableExtra("data") as RepairInfo
        else
            repairInfo = savedInstanceState.getSerializable("data") as RepairInfo

        if (repairInfo == null) {
            finish()
        }

    }

    override fun initListener() {
        ll_select.setOnClickListener(DuplicateClickProxy(this))
        btn_addpick.setOnClickListener(DuplicateClickProxy(this))
        pickingAdapter?.pickingDealListener = object : PickingAdapter.PickingDealListener {
            override fun deletePicking(pos: Int) {
                mPresenter.deletePicking(repairInfo?.ID!!, list[pos].ItemId)
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ll_select -> {
                val selectPickingPopuWindow = SelectPickingPopuWindow(
                    mContext,
                    layoutInflater.inflate(R.layout.popu_select_picking, null),
                    DensityUtil.dp2px(280f),
                    DensityUtil.dp2px(260f),
                    this.wareList,
                    repairInfo!!
                )
                selectPickingPopuWindow.onSelectResultListener =
                    object : SelectPickingPopuWindow.OnSelectResultListener {
                        override fun onResult(wares: Ware) {
                            this@PickingMaterialActivity.currentWareId = wares.WareId.toString()
                            tv_warename.text = wares.WareName
                            mPresenter.changeWareHouse(wares.WareId, repairInfo?.ID!!)
                        }

                    }
                selectPickingPopuWindow.showAtLocation(ll_select, Gravity.CENTER, 0, 0)
            }

            R.id.btn_addpick -> {
                val intent = Intent(
                    mContext,
                    SelectMaterialActivity::class.java
                )
                intent.putExtra("WareId", currentWareId)
                intent.putExtra("RepairId", repairInfo?.ID!!)
                mContext.startActivity(intent)
            }

        }
    }

    override fun downOnRefresh() {
        mPresenter.pickingListData(repairInfo?.ID!!)
    }


    override fun createPresenter(): PickingPresenter {
        return PickingPresenter(this)
    }

    override fun deletePickingSucc() {
        ToastUtil.show("删除成功")
        downOnRefresh()
    }

    override fun onError(mess: String?) {
        super.onError(mess)
        refresh.isRefreshing = false
        isRefresh = false
    }

    override fun wareHouseList(data: PickingWarehouseData) {
        refresh.isRefreshing = false
        isRefresh = false
        wareList = data.WareList
        tv_warename.text = data.CurrentWareName
        this.currentWareId = data.CurrentWareId

        list.clear()
        list.addAll(data.SaleSheetDetailList)
        pickingAdapter?.notifyDataSetChanged()

    }
}
